package models

import (
	"fmt"
	"gorm.io/gorm"
	"travel/appconfig"
)
import "gorm.io/driver/mysql"

var (
	DB  *gorm.DB
	err error
)

func Init() {

	Config := appconfig.Config.Mysql

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		Config.User,
		Config.Password,
		Config.Host,
		Config.Port,
		Config.Database)
	fmt.Println(dsn)

	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql 连接成功")

	err = DB.AutoMigrate(&Travel{})
	if err != nil {
		panic(err)
	}
	fmt.Println("迁移成功")
}
